class Counter:
    def __init__(self,max=5):
        self.count = 0
        self.max = max

    def __iter__(self):
        return self
    
    def __next__(self):
        if self.count < self.max:
            self.count += 1
            return self.count
        else:
            raise StopIteration


c=Counter(10)
for i in c:
    print(i)
iter_c=Counter(10)
print(next(iter_c))



class Rooms:
    def __init__(self,num=5):
        self.room_list = []
        for i in range(num):
            self.room_list.append(dict(id=i,name=f'room{i}'))
    
    def __getitem__(self,index):
        if index >= len(self.room_list):
            raise IndexError(" 超出房间数 ")
        return self.room_list[index]
        
    def __str__(self):
        return str(self.room_list)
    
rooms = Rooms()
print(rooms)
print(rooms[3])


class Chain():
    def __init__(self,path=''):
        self._path = path
    
    def __getattr__(self, path):
        return Chain('%s/%s' % (self._path,path))
        
    @property
    def path(self):
        return self._path
    
    def __call__(self, *args):
        if len(args) == 0:
            return self
        else:
            return Chain('%s/%s' % (self._path,*args))
    
print(Chain().v1.users.login.path)
print(Chain().v1.users.path)
print("/v1/users/:user/queryByName 格式：",Chain().v1.users('kim').queryByName().path)


class Student(object):
    def __init__(self, name):
        self.name = name

    def __call__(self):
        print(f'My name is {self.name}.')

s = Student('Kim')
s()

print("callable(Student('Kim')):",callable(s))
print("callable(Chain()):",callable(Chain()))
print("callable(str):",callable(str))
print("callable('str'):",callable('str'))
print("callable([1, 2, 3])",callable([1, 2, 3]))
print("callable(None)",callable(None))